import * as CONSTANT from '../constants'
import axios from 'axios'
import { fromJS } from 'immutable'






const setlistdata = (data)=>({
    
    list : fromJS(data),
    totalpage :Math.ceil(data.length / 5),
    
})

export const seterrimg = ()=>({
    type: CONSTANT.ERRORCODE
})




//返回一个action对象，返回对象-》则为同步action
export const refreshimg = () => ({
    type : CONSTANT.REFRESHIMG
})

export const setuserinfo =(data) =>({
    type : CONSTANT.SETUSR,
    username : fromJS(data.username),
    identity :  fromJS(data.identity),
    avatar : fromJS(data.avatar)

})

export const setusravatar = (data) => ({
    type : CONSTANT.SETAVATAR,
    avatar : fromJS(data.avatar)
})


//异步action，就是指action的值为函数
export const asyncExample = (data,time) => {
    return ()=>{
        setTimeout(()=>{
              //报错警告：需要使用中间件
        },time)
    }
}

//使用thunk之后actionCreatros可以返回一个方法 dispatch为参数
export const getdatalist = () => {
    
    return (dispatch) => {
        axios.get('/api/list.json').then((res)=>{
            const data = res.data.data
            dispatch(setlistdata(data))
        }).catch( ()=>{
            console.log('error');
    })
    }
}





// export const gettestregister = () => {
    
//     return (dispatch) => {
//         let user_data = {
//             username:"mt_test",
//             password:"fuckuletherman"
//         }
//         request({
//             url: 'kongzi/user/register',
//             method: 'post',
//             data: user_data
            
//         }).then(res=> {
//             console.log(res);
            
//         }).catch(err => {
//             console.log(err)
//         })
        
//     }
// } 